/********************************
* main function
********************************/
#include "Framework.h"
#include <iostream>
#include <signal.h>

using namespace std;

#define TFW_EVENTQUEUE_ID "TFW_EVENTQUEUE_NAME"

/* golbe variable for all process */
static TFW_EventQueue* selfNotifyEventQueue;
static ProcessTable* s_processTable;

/* SIGCHLD handler */
void onsigchlddummy(int signum)
{
	/* send "evStopAllProcess" event to self eventqueue */	
	cout<<"child process killed!"<<endl;
}

/* event handler */
void onSelfEventHandle(TFW_Event* event)
{
	switch(event->kind)
	{
		case evStartAllProcess::ID :
			s_processTable->startAll();
			break;
			
		case evKillAllProcess::ID :
			cout<<"evKillAllProcess"<<endl;
			break;
	}
}

int main(int argc, char* argv[])
{
	/* initialize process table */
	s_processTable = new ProcessTable();
	s_processTable->initializeProcessTable(PROCESS_LIST_FILE);

	/* catch SIGCHLD single */
	struct sigaction act;
	act.sa_handler = onsigchlddummy;
	sigemptyset(&act.sa_mask);
	sigaction(SIGCHLD, &act, NULL);
	
	/* EventDispatcher */
	TFW_EventDispatcher* eventdispatcher = new TFW_EventDispatcher();
	eventdispatcher->initialize();
	
	/* self event queue */
	selfNotifyEventQueue = new TFW_EventQueue(TFW_EVENTQUEUE_ID, eventdispatcher, MAX_MSG_NUM, MAX_SIZE_MSG);
	selfNotifyEventQueue->initialize();
	selfNotifyEventQueue->start();
	selfNotifyEventQueue->registEventHandle(onSelfEventHandle);
	
	/* post "start all process event" to event queue */
	selfNotifyEventQueue->GEN(evStartAllProcess);
	
	/* main loop */
	eventdispatcher->mainloop();
	
	delete eventdispatcher;	
	return 0;
}

